﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinCalcManager;
using Infragistics.Win.CalcEngine;
using System.Collections;
using System.Text.RegularExpressions;
namespace BYManageInfoSystem
{
    public enum InsertType
    {
        /// <summary>
        /// 对其他语句无任何影响 
        /// </summary>
        Common,
        /// <summary>
        /// 当前语句必须为"select count(1) from .."格式，如果存在则继续执行，不存在回滚事务
        /// </summary>
        WithoutContractNo,
        /// <summary>
        /// 当前语句必须为"select count(1) from .."格式，如果不存在则继续执行，存在回滚事务
        /// </summary>

        WithoutContractNoAndFac,
        
        MoreInsertCopy
    }   
    public partial class Frm_FeeSJTruePayTab : DockContent
    {
        private DGBY.BLL.DG_FeeLockTab m_bllDG_FeeLockTab = new DGBY.BLL.DG_FeeLockTab();
        private DGBY.BLL.DG_FeeTruePayLockTab m_bllDG_FeeTruePayLockTab = new DGBY.BLL.DG_FeeTruePayLockTab();

        //private readonly string m_strFacDirect = "FacDirect";
        private string m_strBianHaoTemp = "";
        private bool m_bClearing = false;
        private int m_nFacID = 0;
       // private DGBY.BLL.LogRecTab m_bllLogRecTab = new DGBY.BLL.LogRecTab();
        private InsertType m_InsertType = new InsertType();
        private DGBY.BLL.SJPackingTab m_bllSJPackingTab = new DGBY.BLL.SJPackingTab();
        private DGBY.BLL.SJ_SJFeeTab m_bllSJ_SJFeeTab = new DGBY.BLL.SJ_SJFeeTab();

        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private DGBY.BLL.FeeSJTruePayTab m_bllFeeSJTruePayTab = new DGBY.BLL.FeeSJTruePayTab();
        private DGBY.BLL.FeeBGTruePayTab m_bllFeeBGTruePayTab = new DGBY.BLL.FeeBGTruePayTab();
        private bool m_bInsertFlag = false;
         private int m_nDelIndex = -1;
    
        private DGBY.Model.FeeSJTruePayTab m_modelFeeSJTruePayTab = new DGBY.Model.FeeSJTruePayTab();
        private DGBY.BLL.HxdNoTab m_bllHxdNoTab = new DGBY.BLL.HxdNoTab();
        private DGBY.BLL.FeeSJRecvMoneyPersonTab m_bllFeeSJRecvMoneyPersonTab = new DGBY.BLL.FeeSJRecvMoneyPersonTab();

        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.BYDepartTab m_bllBYDepartTab = new DGBY.BLL.BYDepartTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        

        Hashtable m_SQLStringList = new Hashtable();
        ArrayList m_arrayList = new ArrayList();
        private void AddNewRow()
        {
            if (Frm_Main.CheckPerm(this.Text, "增加"))
            {
                UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
            }
            else
            {
                MessageBox.Show("对不起您没有该权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        private void InitComBox()
        {
            this.comboBoxShowWay.Items.AddRange(new object[] { "最近半年记录", "全部记录" });
            this.comboBoxShowWay.SelectedIndex = 0;

            DataSet ds = m_bllFeeSJRecvMoneyPersonTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxRecvMoneyPerson.DataSource = ds.Tables[0];

                this.comboBoxRecvMoneyPerson.ValueMember = "FeeSJRecvMoneyPersonTabID";
                this.comboBoxRecvMoneyPerson.DisplayMember = "收款人";
                this.comboBoxRecvMoneyPerson.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxRecvMoneyPerson.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxRecvMoneyPerson.SelectedIndex = 0;
            }
        }

        public Frm_FeeSJTruePayTab()
        {
            InitializeComponent();
        }

        private void ShowAllRecordInDataGridView()
        {
            string strTemp = this.comboBoxShowWay.Text;
            if (strTemp == "全部记录")
                ShowAllRecord("");
            else
            {
                DateTime dtNow = System.DateTime.Now.Date.AddDays(-180);
                ShowAllRecord("case when year(HxdNoTab.SBDate) = 1900 then getdate() else HxdNoTab.SBDate end >= '" + dtNow + "' or FeeSJTruePayTab.PaidanRegisterTabid = -1");
            }
        }

        private void ShowAllRecord(string strWhere)
        {
            //DataSet ds = m_bllFeeSJTruePayTab.GetList("SjDate > '2012/01/01'");
            DataSet ds = m_bllFeeSJTruePayTab.GetList(strWhere);
            ultraGrid1.DataSource = null;


            DataSet dsTemp = m_bllFactoryDataTab.GetList("");

            this.ultraDropDownFac.DataSource = dsTemp.Tables[0];
            this.ultraDropDownFac.DisplayMember = "工厂简称";
            this.ultraDropDownFac.ValueMember = "FactoryDataTabID";
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownFac.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns["工厂简称"].Hidden = false;

            dsTemp = m_bllBYDepartTab.GetList("");

            this.ultraDropDownDep.DataSource = dsTemp.Tables[0];
            this.ultraDropDownDep.DisplayMember = "部门名称";
            this.ultraDropDownDep.ValueMember = "BYDepartTabID";
            this.ultraDropDownDep.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownDep.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownDep.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownDep.DisplayLayout.Bands[0].Columns["部门名称"].Hidden = false;

            dsTemp = m_bllBYCompanyTab.GetList("");

            this.ultraDropDownComp.DataSource = dsTemp.Tables[0];
            this.ultraDropDownComp.DisplayMember = "经营单位简称";
            this.ultraDropDownComp.ValueMember = "BYCompanyTabID";
            this.ultraDropDownComp.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownComp.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownComp.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownComp.DisplayLayout.Bands[0].Columns["经营单位简称"].Hidden = false;


            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns[2].Hidden = true;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["实付费用计提年月"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收金额公式"].Hidden = true;
            //ultraGrid1.DisplayLayout.Bands[0].Columns[ultraGrid1.DisplayLayout.Bands[0].Columns.Count - 1].Hidden = true;
            //ultraGrid1.DisplayLayout.Bands[0].Columns[ultraGrid1.DisplayLayout.Bands[0].Columns.Count - 2].Hidden = true;

            this.ultraGrid1.DisplayLayout.Bands[0].Columns["经营单位"].ValueList = this.ultraDropDownComp;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["部门"].ValueList = this.ultraDropDownDep;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["工厂简称"].ValueList = this.ultraDropDownFac;


        }
        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }
        public static void ultraGrid1_InitializeRowFilter(Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();
            // FILTER ROW FUNCTIONALITY RELATED ULTRAGRID SETTINGS
            //e.Layout.Override.RowFilterAction = RowFilterAction.DisableFilteredOutRows;
            // ----------------------------------------------------------------------------------
            // Enable the the filter row user interface by setting the FilterUIType to FilterRow.
            e.Layout.Override.FilterUIType = FilterUIType.FilterRow;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // FilterEvaluationTrigger specifies when UltraGrid applies the filter criteria typed 
            // into a filter row. Default is OnCellValueChange which will cause the UltraGrid to
            // re-filter the data as soon as the user modifies the value of a filter cell.
            e.Layout.Override.FilterEvaluationTrigger = FilterEvaluationTrigger.OnCellValueChange;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // By default the UltraGrid selects the type of the filter operand editor based on
            // the column's DataType. For DateTime and boolean columns it uses the column's editors.
            // For other column types it uses the Combo. You can explicitly specify the operand
            // editor style by setting the FilterOperandStyle on the override or the individual
            // columns.

            e.Layout.Override.FilterOperandStyle = FilterOperandStyle.DropDownList;

            // By default UltraGrid displays user interface for selecting the filter operator. 
            // You can set the FilterOperatorLocation to hide this user interface. This
            // property is available on column as well so it can be controlled on a per column
            // basis. Default is WithOperand. This property is exposed off the column as well.
            e.Layout.Override.FilterOperatorLocation = FilterOperatorLocation.WithOperand;

            // By default the UltraGrid uses StartsWith as the filter operator. You use
            // the FilterOperatorDefaultValue property to specify a different filter operator
            // to use. This is the default or the initial filter operator value of the cells
            // in filter row. If filter operator user interface is enabled (FilterOperatorLocation
            // is not set to None) then that ui will be initialized to the value of this
            // property. The user can then change the operator as he/she chooses via the operator
            // drop down.
            e.Layout.Override.FilterOperatorDefaultValue = FilterOperatorDefaultValue.StartsWith;

            // FilterOperatorDropDownItems property can be used to control the options provided
            // to the user for selecting the filter operator. By default UltraGrid bases 
            // what operator options to provide on the column's data type. This property is
            // avaibale on the column as well.
            //e.Layout.Override.FilterOperatorDropDownItems = FilterOperatorDropDownItems.All;

            // By default UltraGrid displays a clear button in each cell of the filter row
            // as well as in the row selector of the filter row. When the user clicks this
            // button the associated filter criteria is cleared. You can use the 
            // FilterClearButtonLocation property to control if and where the filter clear
            // buttons are displayed.
            e.Layout.Override.FilterClearButtonLocation = FilterClearButtonLocation.RowAndCell;

            // Appearance of the filter row can be controlled using the FilterRowAppearance proeprty.
            e.Layout.Override.FilterRowAppearance.BackColor = Color.LightYellow;

            // You can use the FilterRowPrompt to display a prompt in the filter row. By default
            // UltraGrid does not display any prompt in the filter row.
            e.Layout.Override.FilterRowPrompt = "Click here to filter data...";

            // You can use the FilterRowPromptAppearance to change the appearance of the prompt.
            // By default the prompt is transparent and uses the same fore color as the filter row.
            // You can make it non-transparent by setting the appearance' BackColorAlpha property 
            // or by setting the BackColor to a desired value.
            e.Layout.Override.FilterRowPromptAppearance.BackColorAlpha = Alpha.Opaque;

            // By default the prompt is spread across multiple cells if it's bigger than the
            // first cell. You can confine the prompt to a particular cell by setting the
            // SpecialRowPromptField property off the band to the key of a column.
            //e.Layout.Bands[0].SpecialRowPromptField = e.Layout.Bands[0].Columns[0].Key;

            // Display a separator between the filter row other rows. SpecialRowSeparator property 
            // can be used to display separators between various 'special' rows, including for the
            // filter row. This property is a flagged enum property so it can take multiple values.
            e.Layout.Override.SpecialRowSeparator = SpecialRowSeparator.FilterRow;

            // You can control the appearance of the separator using the SpecialRowSeparatorAppearance
            // property.
            e.Layout.Override.SpecialRowSeparatorAppearance.BackColor = Color.FromArgb(233, 242, 199);

            //   UltraGridBand band = e.Layout.Bands[0];
            //   band.ColumnFilters["原币金额"].FilterConditions.Clear();
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.GreaterThan, 5);
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.LessThan, 10);
            //   band.ColumnFilters["原币金额"].LogicalOperator = FilterLogicalOperator.And;
        }
        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["编号是否手动生成"].Width = 30;
            ultraGrid1.DisplayLayout.Bands[0].Columns["实付日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对公费用编号"].Width = 130;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对私费用编号"].Width = 130;
            ultraGrid1.DisplayLayout.Bands[0].Columns["是否直接开票"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["录入日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["商检日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["申报日期"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["申报年月"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经营单位"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["部门"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂简称"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂生产货品种类"].Width = 45;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同编号"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经营单位"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["报关柜数"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收金额"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收金额公式"].Width = 60; 
            ultraGrid1.DisplayLayout.Bands[0].Columns["对公公式"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对私公式"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经办人"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["商检收据号"].Width = 70;
            ultraGrid1.DisplayLayout.Bands[0].Columns["收款人"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收费用计提年月"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["实付费用计提年月"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].Width = 100;
        }
        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ultraGrid1.DisplayLayout.Override.WrapHeaderText = Infragistics.Win.DefaultableBoolean.True;

            InitColumnWidth();
            SummarySettings averageSummary =
e.Layout.Bands[0].Summaries.Add(
"应付实付金额",
SummaryType.Sum,
e.Layout.Bands[0].Columns["应付实付金额"]);
            averageSummary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;
            averageSummary.SummaryPositionColumn = averageSummary.SourceColumn;
            averageSummary.DisplayFormat = "{0:.##}";
            averageSummary.Band.Override.SummaryFooterCaptionVisible = DefaultableBoolean.False;
            averageSummary.Band.Override.BorderStyleSummaryFooter = UIElementBorderStyle.None;
            averageSummary.SummaryDisplayArea = SummaryDisplayAreas.TopFixed;


            averageSummary =
e.Layout.Bands[0].Summaries.Add(
"应收金额",
SummaryType.Sum,
e.Layout.Bands[0].Columns["应收金额"]);
            averageSummary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;
            averageSummary.SummaryPositionColumn = averageSummary.SourceColumn;
            averageSummary.DisplayFormat = "{0:.##}";
            averageSummary.Band.Override.SummaryFooterCaptionVisible = DefaultableBoolean.False;
            averageSummary.Band.Override.BorderStyleSummaryFooter = UIElementBorderStyle.None;
            averageSummary.SummaryDisplayArea = SummaryDisplayAreas.TopFixed;

            if (this.checkBox1.Checked == true)
                ultraGrid1_InitializeRowFilter(e);
            else
                ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            //ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            //ClassUltraGridCommonSet.LocalPrintPreveiw();
            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "合同编号"|| str == "应付实付金额" || str == "申报日期"
                    || str == "对公费用编号" || str == "对私费用编号" || str == "报关柜数"
                    || str == "录入日期" || str == "商检日期" || str == "经办人" || str == "实付日期")
                {
                    column2.CellAppearance.BackColor = Color.Coral;
                    column2.CellActivation = Activation.ActivateOnly;
                }
            }

            //this.ultraGrid1.DisplayLayout.Bands[0].Columns.
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    AddNewRow();
                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        //if (strTemp == "经营公司" | strTemp == "客户" | strTemp == "币种" | strTemp == "收款方式")
                        //    return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        //if(nIndex != 0)
                            
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        //if (strTemp == "经营公司" | strTemp == "客户" | strTemp == "币种" | strTemp == "收款方式")
                        //    return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }
        private bool CheckModify(UltraGridRow CurrentUGR)
        {
            int nYearMonth = Convert.ToInt32(CurrentUGR.Cells["应收费用计提年月"].Value.ToString(), 10);
            DataSet ds = m_bllDG_FeeLockTab.GetList("FeeName = '商检' and IsLock = 1 and LockYearMonth = '" + nYearMonth.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return false;
            }
            return true;
        }
        private bool IsFeeTruePayLocked(UltraGridRow CurrentUGR)
        {
            int nYearMonth = Convert.ToInt32(CurrentUGR.Cells["实付费用计提年月"].Value.ToString(), 10);
            DataSet ds = m_bllDG_FeeTruePayLockTab.GetList("FeeName = '商检' and IsLock = 1 and LockYearMonth = '" + nYearMonth.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        private bool CheckFeeTruePayModify(DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTab)
        {
            DGBY.Model.FeeSJTruePayTab oldModelFeeSJTruePayTab = m_bllFeeSJTruePayTab.GetModel(modelFeeSJTruePayTab.FeeSJTruePayTabID);
            if (null != oldModelFeeSJTruePayTab)
            {
                //限制修改实付日期、费用编号、是否直接开票、应付实付金额
                if (oldModelFeeSJTruePayTab.TruePayDate != modelFeeSJTruePayTab.TruePayDate
                    || oldModelFeeSJTruePayTab.bitFiledReserv1 != modelFeeSJTruePayTab.bitFiledReserv1
                    || oldModelFeeSJTruePayTab.ShouldRecvNotRecvMoney != modelFeeSJTruePayTab.ShouldRecvNotRecvMoney
                    || oldModelFeeSJTruePayTab.ToPubBianHao != modelFeeSJTruePayTab.ToPubBianHao
                    || oldModelFeeSJTruePayTab.ToPrivateBianHao != modelFeeSJTruePayTab.ToPrivateBianHao)
                    return true;
            }
            
            return false;
        }
        private bool ModifyGoodsDetail(int nRowIndex)
        {
            try
            {
                if (!Frm_Main.CheckPerm(this.Text, "修改"))
                {
                    MessageBox.Show("对不起您没有该权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (m_bClearing)
                {
                    m_bClearing = false;
                    return false;
                }
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];


                bool bRet = CheckModify(CurrentUGR);
                if (!bRet)
                {
                    MessageBox.Show("费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }

                int nFeeSJTruePayTabID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTab
                    = m_bllFeeSJTruePayTab.GetModel(nFeeSJTruePayTabID);

                //if (this.textBoxSJShouJuNo.Text == "" && modelFeeSJTruePayTab.SJShoujuNo == "")
                //{
                //    MessageBox.Show("请设定商检收据号！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                //    return false;
                //}
                //string strContractNo = CurrentUGR.Cells["合同编号"].Value.ToString();

                int nPaidanRegisterTabID = (int)CurrentUGR.Cells["PaidanRegisterTabID"].Value;
                int nFactoryDataTabID = (int)CurrentUGR.Cells["FactoryDataTabID"].Value;

                if (nFactoryDataTabID > 0 && nPaidanRegisterTabID == -1)
                {
                    DataSet ds;
                    string strBGH = CurrentUGR.Cells["工厂简称"].Value.ToString();
                    if (ClassInputValid.IsNum(strBGH))
                    {
                        modelFeeSJTruePayTab.FactoryDataTabID = Convert.ToInt32(CurrentUGR.Cells["工厂简称"].Value.ToString(), 10);
                    }
                    else
                    {
                        ds = m_bllFactoryDataTab.GetList("FactoryNameJX = '" + strBGH + "'");
                        if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                        {
                            modelFeeSJTruePayTab.FactoryDataTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["FactoryDataTabID"].ToString(), 10);
                        }
                    }

                    ds = m_bllFactoryDataTab.GetListEx("FactoryDataTabID = '" + modelFeeSJTruePayTab.FactoryDataTabID + "'");
                    modelFeeSJTruePayTab.BYCompanyTabID = (int)ds.Tables[0].Rows[0]["BYCompanyTabID"];
                    modelFeeSJTruePayTab.BYDepartTabID = (int)ds.Tables[0].Rows[0]["BYDepartTabID"];
                }
                else if (nFactoryDataTabID == -1 && nPaidanRegisterTabID == -1)
                {
                    string strBGH = CurrentUGR.Cells["经营单位"].Value.ToString();
                    if (ClassInputValid.IsNum(strBGH))
                    {
                        modelFeeSJTruePayTab.BYCompanyTabID = Convert.ToInt32(CurrentUGR.Cells["经营单位"].Value.ToString(), 10);
                    }
                    else
                    {
                        DataSet ds = m_bllBYCompanyTab.GetList("CompanyNameJX = '" + strBGH + "'");
                        if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                        {
                            modelFeeSJTruePayTab.BYCompanyTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BYCompanyTabID"].ToString(), 10);
                        }
                    }


                    strBGH = CurrentUGR.Cells["部门"].Value.ToString();
                    if (ClassInputValid.IsNum(strBGH))
                    {
                        modelFeeSJTruePayTab.BYDepartTabID = Convert.ToInt32(CurrentUGR.Cells["部门"].Value.ToString(), 10);
                    }
                    else
                    {
                        DataSet ds = m_bllBYDepartTab.GetList("DepartName = '" + strBGH + "'");
                        if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                        {
                            modelFeeSJTruePayTab.BYDepartTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BYDepartTabID"].ToString(), 10);
                        }
                    }
                }

                if(CurrentUGR.Cells["实付日期"].Text != "")
                {
                    DateTime dtTemp = Convert.ToDateTime(CurrentUGR.Cells["实付日期"].Value.ToString());
                    modelFeeSJTruePayTab.TruePayDate = dtTemp.Date;
                }
                
                if (modelFeeSJTruePayTab.ToPrivateBianHao == "" && modelFeeSJTruePayTab.ToPubBianHao == "")
                    modelFeeSJTruePayTab.InputDate = System.DateTime.Now.Date; 

                modelFeeSJTruePayTab.TruePayMonth = Convert.ToInt32(CurrentUGR.Cells["实付费用计提年月"].Value.ToString(), 10);
                modelFeeSJTruePayTab.ShouldPayMonth = Convert.ToInt32(CurrentUGR.Cells["应收费用计提年月"].Value.ToString(), 10);
         //       modelFeeSJTruePayTab.ShouldRecvNotRecvMoney = Convert.ToDecimal(CurrentUGR.Cells["应付实付金额"].Value.ToString()); ;

                modelFeeSJTruePayTab.ToPubFormula = CurrentUGR.Cells["对公公式"].Value.ToString();
                modelFeeSJTruePayTab.ToPrivateFormula = CurrentUGR.Cells["对私公式"].Value.ToString();
                if (CurrentUGR.Cells["对公公式"].Value.ToString() != ""
                    && CurrentUGR.Cells["对私公式"].Value.ToString() != "")
                {
                    MessageBox.Show("对公公式,对私公式不能同时添加！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }

                modelFeeSJTruePayTab.JinBanPerson = modelFeeSJTruePayTab.JinBanPerson != "" 
                    ? CurrentUGR.Cells["经办人"].Value.ToString()
                    : Frm_Main.m_strLoginUser;
                modelFeeSJTruePayTab.RecvMonyePerson = modelFeeSJTruePayTab.RecvMonyePerson != ""
                    ? CurrentUGR.Cells["收款人"].Value.ToString()
                    : this.comboBoxRecvMoneyPerson.Text;

                modelFeeSJTruePayTab.SJShoujuNo = modelFeeSJTruePayTab.SJShoujuNo != ""
       ? CurrentUGR.Cells["商检收据号"].Value.ToString()
       : this.textBoxSJShouJuNo.Text;

                //modelFeeSJTruePayTab.IsToRecvPayTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至应收应付登记表"].Value.ToString(), 10);
                //modelFeeSJTruePayTab.IsToFinCheckMoneyTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至财务对帐"].Value.ToString(), 10);
                modelFeeSJTruePayTab.Note = CurrentUGR.Cells["备注"].Value.ToString();


                if (modelFeeSJTruePayTab.ToPubFormula != "")
                    modelFeeSJTruePayTab.ShouldRecvNotRecvMoney
                        = this.ultraCalcManager1.Calculate(modelFeeSJTruePayTab.ToPubFormula).ToDecimal();
                else if (modelFeeSJTruePayTab.ToPrivateFormula != "")
                    modelFeeSJTruePayTab.ShouldRecvNotRecvMoney
                        = this.ultraCalcManager1.Calculate(modelFeeSJTruePayTab.ToPrivateFormula).ToDecimal();
                else
                    modelFeeSJTruePayTab.ShouldRecvNotRecvMoney = 0;


                modelFeeSJTruePayTab.bitFiledReserv1 = (bool)CurrentUGR.Cells["是否直接开票"].Value;
          
                // int nGuiCount = Convert.ToInt32(CurrentUGR.Cells["商检柜数"].Value.ToString(), 10);



                //if (strProductCate == "家具")
                //    CurrentUGR.Cells["应收金额"].Value = nGuiCount * 200;
                //else
                //    CurrentUGR.Cells["应收金额"].Value = modelFeeSJTruePayTab.ShouldRecvNotRecvMoney == 0 ? 0 : modelFeeSJTruePayTab.ShouldRecvNotRecvMoney+ 20;

                CurrentUGR.Cells["应付实付金额"].Value = modelFeeSJTruePayTab.ShouldRecvNotRecvMoney;
                CurrentUGR.Cells["收款人"].Value = modelFeeSJTruePayTab.RecvMonyePerson;
                CurrentUGR.Cells["经办人"].Value = modelFeeSJTruePayTab.JinBanPerson;
                CurrentUGR.Cells["商检收据号"].Value = modelFeeSJTruePayTab.SJShoujuNo;
                CurrentUGR.Cells["录入日期"].Value = modelFeeSJTruePayTab.InputDate;

                bRet = IsFeeTruePayLocked(CurrentUGR) && CheckFeeTruePayModify(modelFeeSJTruePayTab);
                if (bRet)
                {
                    MessageBox.Show("实付费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }

                m_bllFeeSJTruePayTab.Update(modelFeeSJTruePayTab);
                DataSet ds2 = m_bllFeeSJTruePayTab.GetList("FeeSJTruePayTabID = '" + nFeeSJTruePayTabID.ToString() + "'");

                Decimal dtModelShouldRecvMoneyOld = modelFeeSJTruePayTab.ShouldRecvMoney;
                Decimal dtShouldRecvMoneyOld = (Decimal)CurrentUGR.Cells["应收金额"].Value;

                if (dtShouldRecvMoneyOld == 0)
                {
                    if (modelFeeSJTruePayTab.FactoryDataTabID != 282)//天朗
                    {
                        if(dtModelShouldRecvMoneyOld == 0)
                            CurrentUGR.Cells["应收金额"].Value = ds2.Tables[0].Rows[0]["应收金额公式"];
                    }
                    else
                        CurrentUGR.Cells["应收金额"].Value = modelFeeSJTruePayTab.ShouldRecvNotRecvMoney;

                }
                else
                {
                    if (modelFeeSJTruePayTab.FactoryDataTabID != 282)//天朗
                    {
                        if (dtShouldRecvMoneyOld == (Decimal)ds2.Tables[0].Rows[0]["应收金额公式"])
                        {
                            CurrentUGR.Cells["应收金额"].Value = ds2.Tables[0].Rows[0]["应收金额公式"];
                        }

                    }
                    else
                    {
                        if (dtShouldRecvMoneyOld == modelFeeSJTruePayTab.ShouldRecvNotRecvMoney)
                        {
                            CurrentUGR.Cells["应收金额"].Value = modelFeeSJTruePayTab.ShouldRecvNotRecvMoney;
                        }
                    }
                }

                modelFeeSJTruePayTab.ShouldRecvMoney = Convert.ToDecimal(CurrentUGR.Cells["应收金额"].Value.ToString());

                m_bllFeeSJTruePayTab.Update(modelFeeSJTruePayTab);
                Frm_Main.Log2DB(this.Text, "修改", CurrentUGR.Cells["合同编号"].Value.ToString());
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ResetRecord(int nFeeSJTruePayTabID)
        {

            try
            {
                DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTab
                    = m_bllFeeSJTruePayTab.GetModel(nFeeSJTruePayTabID);


                modelFeeSJTruePayTab.ToPubBianHao = "";
                modelFeeSJTruePayTab.ToPrivateBianHao = "";
                modelFeeSJTruePayTab.SJShoujuNo = "";
                modelFeeSJTruePayTab.TruePayDate = null;
                modelFeeSJTruePayTab.InputDate = null; 
                modelFeeSJTruePayTab.TruePayMonth = 0;
                modelFeeSJTruePayTab.ShouldPayMonth = 0;
                modelFeeSJTruePayTab.ShouldRecvNotRecvMoney = 0;
                modelFeeSJTruePayTab.ShouldRecvMoney = 0;

                modelFeeSJTruePayTab.ToPubFormula = "";
                modelFeeSJTruePayTab.ToPrivateFormula = "";

                modelFeeSJTruePayTab.JinBanPerson = "";
                modelFeeSJTruePayTab.RecvMonyePerson = "";

                modelFeeSJTruePayTab.IsToRecvPayTab = 0;
                modelFeeSJTruePayTab.IsToFinCheckMoneyTab = 0;
                modelFeeSJTruePayTab.Note = "";
                //modelFeeSJTruePayTab.SjDate = System.DateTime.Now.Date;

                modelFeeSJTruePayTab.bitFiledReserv1 = false;
                modelFeeSJTruePayTab.bitFiledReserv2 = false;

                modelFeeSJTruePayTab.stringFiledReserv1 = "";
                modelFeeSJTruePayTab.stringFiledReserv2 = "";
                modelFeeSJTruePayTab.stringFiledReserv3 = "";
                modelFeeSJTruePayTab.stringFiledReserv4 = "";

                modelFeeSJTruePayTab.intFiledReserv1 = 0;
                modelFeeSJTruePayTab.intFiledReserv2 = 0;
                modelFeeSJTruePayTab.intFiledReserv3 = 0;

                DateTime dtNow = System.DateTime.Now.Date;
                modelFeeSJTruePayTab.datetimeFiledReserv1 = dtNow;
                modelFeeSJTruePayTab.datetimeFiledReserv2 = dtNow;

                modelFeeSJTruePayTab.decimalFiledReserv1 = 0;
                modelFeeSJTruePayTab.decimalFiledReserv2 = 0;
                modelFeeSJTruePayTab.decimalFiledReserv3 = 0;
                modelFeeSJTruePayTab.decimalFiledReserv4 = 0;

                m_bllFeeSJTruePayTab.Update(modelFeeSJTruePayTab);

                Frm_Main.Log2DB(this.Text, "清空", ultraGrid1.ActiveRow.Cells["合同编号"].Value.ToString());
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }
        }


        private void ResetUltraGridRow(int nRowIndex)
        {

            try
            {

                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
         
                CurrentUGR.Cells["对公费用编号"].Value = "";
                CurrentUGR.Cells["对私费用编号"].Value = "";
  
                CurrentUGR.Cells["实付日期"].Value = DBNull.Value;

                CurrentUGR.Cells["录入日期"].Value = DBNull.Value;

                CurrentUGR.Cells["实付费用计提年月"].Value = 0;
                CurrentUGR.Cells["应收费用计提年月"].Value = 0;
                CurrentUGR.Cells["应付实付金额"].Value = 0;
                CurrentUGR.Cells["应收金额"].Value = 0;

                CurrentUGR.Cells["对公公式"].Value = "";
                CurrentUGR.Cells["对私公式"].Value = "";
                CurrentUGR.Cells["经办人"].Value = "";
                CurrentUGR.Cells["收款人"].Value = "";
                CurrentUGR.Cells["商检收据号"].Value = "";
                CurrentUGR.Cells["是否直接开票"].Value = false;
                //CurrentUGR.Cells["是否已提取至应收应付登记表"].Value = m_modelFeeSJTruePayTab.IsToRecvPayTab;
                //CurrentUGR.Cells["是否已提取至财务对帐"].Value = m_modelFeeSJTruePayTab.IsToFinCheckMoneyTab;


                CurrentUGR.Cells["备注"].Value = "";


            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid1.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;

                }
                else//修改
                {

                    ModifyGoodsDetail(e.Row.Index);

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }
        private bool AddGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTab = new DGBY.Model.FeeSJTruePayTab();
                modelFeeSJTruePayTab = m_modelFeeSJTruePayTab;
                modelFeeSJTruePayTab.ToPrivateBianHao = "";
                modelFeeSJTruePayTab.ToPubBianHao = "";
                modelFeeSJTruePayTab.TruePayDate = null;

                DGBY.Model.PaidanRegisterTab modelPaidanRegisterTab = m_bllPaidanRegisterTab.GetModel(modelFeeSJTruePayTab.PaidanRegisterTabID);
               
                m_bllFeeSJTruePayTab.Add(modelFeeSJTruePayTab);
                //ShowAllRecordInDataGridView();
                if (modelPaidanRegisterTab != null)
                    Frm_Main.Log2DB(this.Text, "添加", modelPaidanRegisterTab.ContractCode);
                else
                    Frm_Main.Log2DB(this.Text, "添加", modelFeeSJTruePayTab.FeeSJTruePayTabID.ToString());
                ShowAllRecord("FeeSJTruePayTab.PaidanRegisterTabID = '" + modelFeeSJTruePayTab.PaidanRegisterTabID.ToString() + "'");
                
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private bool AddGoodsDetailNoContractNo(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTab = new DGBY.Model.FeeSJTruePayTab();
             
                modelFeeSJTruePayTab.ToPrivateBianHao = "";
                modelFeeSJTruePayTab.ToPubBianHao = "";
                modelFeeSJTruePayTab.TruePayDate = null;
                modelFeeSJTruePayTab.BYCompanyTabID = 1;
                modelFeeSJTruePayTab.BYDepartTabID = 1;
                modelFeeSJTruePayTab.FactoryDataTabID = 1;
                modelFeeSJTruePayTab.PaidanRegisterTabID = -1;

                DateTime dtNow = System.DateTime.Now.Date;
                modelFeeSJTruePayTab.SjDate = dtNow;
                modelFeeSJTruePayTab.InputDate = dtNow;
                modelFeeSJTruePayTab.TruePayMonth = 1;
                modelFeeSJTruePayTab.ShouldPayMonth = 1;

                modelFeeSJTruePayTab.ToPubFormula ="";
                modelFeeSJTruePayTab.ToPrivateFormula = "";
               

                modelFeeSJTruePayTab.JinBanPerson = "";
                modelFeeSJTruePayTab.RecvMonyePerson = "";

                modelFeeSJTruePayTab.SJShoujuNo = "";

                //modelFeeSJTruePayTab.IsToRecvPayTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至应收应付登记表"].Value.ToString(), 10);
                //modelFeeSJTruePayTab.IsToFinCheckMoneyTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至财务对帐"].Value.ToString(), 10);
                modelFeeSJTruePayTab.Note = "";



                modelFeeSJTruePayTab.ShouldRecvNotRecvMoney = 0;


                modelFeeSJTruePayTab.ShouldRecvMoney = 0;

                modelFeeSJTruePayTab.bitFiledReserv1 = false;
                modelFeeSJTruePayTab.bitFiledReserv2 = false;

                modelFeeSJTruePayTab.stringFiledReserv1 = "";
                modelFeeSJTruePayTab.stringFiledReserv2 = "";
                modelFeeSJTruePayTab.stringFiledReserv3 = "";
                modelFeeSJTruePayTab.stringFiledReserv4 = "";

                modelFeeSJTruePayTab.intFiledReserv1 = 0;
                modelFeeSJTruePayTab.intFiledReserv2 = 0;
                modelFeeSJTruePayTab.intFiledReserv3 = 0;

                modelFeeSJTruePayTab.datetimeFiledReserv1 = dtNow;
                modelFeeSJTruePayTab.datetimeFiledReserv2 = dtNow;

                modelFeeSJTruePayTab.decimalFiledReserv1 = 0;
                modelFeeSJTruePayTab.decimalFiledReserv2 = 0;
                modelFeeSJTruePayTab.decimalFiledReserv3 = 0;
                modelFeeSJTruePayTab.decimalFiledReserv4 = 0;


                int nID = m_bllFeeSJTruePayTab.Add(modelFeeSJTruePayTab);
                Frm_Main.Log2DB(this.Text, "增加无合同号记录", nID.ToString());
                ShowAllRecord("FeeSJTruePayTab.FeeSJTruePayTabID = '" + nID.ToString() + "'");


                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private bool AddGoodsDetailNoContractNoAndFac(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTab = new DGBY.Model.FeeSJTruePayTab();

                modelFeeSJTruePayTab.ToPrivateBianHao = "";
                modelFeeSJTruePayTab.ToPubBianHao = "";
                modelFeeSJTruePayTab.TruePayDate = null;
                modelFeeSJTruePayTab.BYCompanyTabID = 1;
                modelFeeSJTruePayTab.BYDepartTabID = 1;
                modelFeeSJTruePayTab.FactoryDataTabID = -1;
                modelFeeSJTruePayTab.PaidanRegisterTabID = -1;

                DateTime dtNow = System.DateTime.Now.Date;
                modelFeeSJTruePayTab.SjDate = dtNow;
                modelFeeSJTruePayTab.InputDate = dtNow;
                modelFeeSJTruePayTab.TruePayMonth = 1;
                modelFeeSJTruePayTab.ShouldPayMonth = 1;

                modelFeeSJTruePayTab.ToPubFormula = "";
                modelFeeSJTruePayTab.ToPrivateFormula = "";


                modelFeeSJTruePayTab.JinBanPerson = "";
                modelFeeSJTruePayTab.RecvMonyePerson = "";

                modelFeeSJTruePayTab.SJShoujuNo = "";

                //modelFeeSJTruePayTab.IsToRecvPayTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至应收应付登记表"].Value.ToString(), 10);
                //modelFeeSJTruePayTab.IsToFinCheckMoneyTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至财务对帐"].Value.ToString(), 10);
                modelFeeSJTruePayTab.Note = "";



                modelFeeSJTruePayTab.ShouldRecvNotRecvMoney = 0;


                modelFeeSJTruePayTab.ShouldRecvMoney = 0;

                modelFeeSJTruePayTab.bitFiledReserv1 = false;
                modelFeeSJTruePayTab.bitFiledReserv2 = false;

                modelFeeSJTruePayTab.stringFiledReserv1 = "";
                modelFeeSJTruePayTab.stringFiledReserv2 = "";
                modelFeeSJTruePayTab.stringFiledReserv3 = "";
                modelFeeSJTruePayTab.stringFiledReserv4 = "";

                modelFeeSJTruePayTab.intFiledReserv1 = 0;
                modelFeeSJTruePayTab.intFiledReserv2 = 0;
                modelFeeSJTruePayTab.intFiledReserv3 = 0;

                modelFeeSJTruePayTab.datetimeFiledReserv1 = dtNow;
                modelFeeSJTruePayTab.datetimeFiledReserv2 = dtNow;

                modelFeeSJTruePayTab.decimalFiledReserv1 = 0;
                modelFeeSJTruePayTab.decimalFiledReserv2 = 0;
                modelFeeSJTruePayTab.decimalFiledReserv3 = 0;
                modelFeeSJTruePayTab.decimalFiledReserv4 = 0;

                //m_bllFeeSJTruePayTab.Add(modelFeeSJTruePayTab);
                //ShowAllRecordInDataGridView();
                int nID = m_bllFeeSJTruePayTab.Add(modelFeeSJTruePayTab);
                Frm_Main.Log2DB(this.Text, "增加无工厂及合同号", nID.ToString());
                ShowAllRecord("FeeSJTruePayTab.FeeSJTruePayTabID = '" + nID.ToString() + "'");

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private void InitNewAddRowValueCopyContract(int nIndex)
        {

            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["合同编号"].Value = m_modelFeeSJTruePayTab.PaidanRegisterTabID;
            CurrentUGR.Cells["对公费用编号"].Value = m_modelFeeSJTruePayTab.ToPubBianHao;
            CurrentUGR.Cells["对私费用编号"].Value = m_modelFeeSJTruePayTab.ToPrivateBianHao;
            if(m_modelFeeSJTruePayTab.TruePayDate != null)
                CurrentUGR.Cells["实付日期"].Value = m_modelFeeSJTruePayTab.TruePayDate;
            if (m_modelFeeSJTruePayTab.InputDate != null)
                CurrentUGR.Cells["录入日期"].Value = m_modelFeeSJTruePayTab.InputDate;

            CurrentUGR.Cells["实付费用计提年月"].Value = m_modelFeeSJTruePayTab.TruePayMonth;
            CurrentUGR.Cells["应收费用计提年月"].Value = m_modelFeeSJTruePayTab.ShouldPayMonth;
            CurrentUGR.Cells["应付实付金额"].Value = m_modelFeeSJTruePayTab.ShouldRecvNotRecvMoney;

            CurrentUGR.Cells["对公公式"].Value = m_modelFeeSJTruePayTab.ToPubFormula;
            CurrentUGR.Cells["对私公式"].Value = m_modelFeeSJTruePayTab.ToPrivateFormula;
            CurrentUGR.Cells["经办人"].Value = m_modelFeeSJTruePayTab.JinBanPerson;
            CurrentUGR.Cells["收款人"].Value = m_modelFeeSJTruePayTab.RecvMonyePerson;

            //CurrentUGR.Cells["是否已提取至应收应付登记表"].Value = m_modelFeeSJTruePayTab.IsToRecvPayTab;
            //CurrentUGR.Cells["是否已提取至财务对帐"].Value = m_modelFeeSJTruePayTab.IsToFinCheckMoneyTab;


            CurrentUGR.Cells["备注"].Value = m_modelFeeSJTruePayTab.Note;

            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetail(nIndex);

            }

        }

        private void InitNewAddRowValueNoContract(int nIndex)
        {

            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["合同编号"].Value = "";
            CurrentUGR.Cells["对公费用编号"].Value = m_modelFeeSJTruePayTab.ToPubBianHao;
            CurrentUGR.Cells["对私费用编号"].Value = m_modelFeeSJTruePayTab.ToPrivateBianHao;
            if (m_modelFeeSJTruePayTab.TruePayDate != null)
                CurrentUGR.Cells["实付日期"].Value = m_modelFeeSJTruePayTab.TruePayDate;
            if (m_modelFeeSJTruePayTab.InputDate != null)
                CurrentUGR.Cells["录入日期"].Value = m_modelFeeSJTruePayTab.InputDate;

            CurrentUGR.Cells["实付费用计提年月"].Value = m_modelFeeSJTruePayTab.TruePayMonth;
            CurrentUGR.Cells["应收费用计提年月"].Value = m_modelFeeSJTruePayTab.ShouldPayMonth;
            CurrentUGR.Cells["应付实付金额"].Value = m_modelFeeSJTruePayTab.ShouldRecvNotRecvMoney;

            CurrentUGR.Cells["对公公式"].Value = m_modelFeeSJTruePayTab.ToPubFormula;
            CurrentUGR.Cells["对私公式"].Value = m_modelFeeSJTruePayTab.ToPrivateFormula;
            CurrentUGR.Cells["经办人"].Value = m_modelFeeSJTruePayTab.JinBanPerson;
            CurrentUGR.Cells["收款人"].Value = m_modelFeeSJTruePayTab.RecvMonyePerson;

            //CurrentUGR.Cells["是否已提取至应收应付登记表"].Value = m_modelFeeSJTruePayTab.IsToRecvPayTab;
            //CurrentUGR.Cells["是否已提取至财务对帐"].Value = m_modelFeeSJTruePayTab.IsToFinCheckMoneyTab;


            CurrentUGR.Cells["备注"].Value = m_modelFeeSJTruePayTab.Note;

            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetailNoContractNo(nIndex);

            }

        }

        private void InitNewAddRowValueNoContractAndFac(int nIndex)
        {

            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["合同编号"].Value = "";
            CurrentUGR.Cells["工厂简称"].Value = "";
            CurrentUGR.Cells["对公费用编号"].Value = m_modelFeeSJTruePayTab.ToPubBianHao;
            CurrentUGR.Cells["对私费用编号"].Value = m_modelFeeSJTruePayTab.ToPrivateBianHao;
            if (m_modelFeeSJTruePayTab.TruePayDate != null)
                CurrentUGR.Cells["实付日期"].Value = m_modelFeeSJTruePayTab.TruePayDate;
            if (m_modelFeeSJTruePayTab.InputDate != null)
                CurrentUGR.Cells["录入日期"].Value = m_modelFeeSJTruePayTab.InputDate;

            CurrentUGR.Cells["实付费用计提年月"].Value = m_modelFeeSJTruePayTab.TruePayMonth;
            CurrentUGR.Cells["应收费用计提年月"].Value = m_modelFeeSJTruePayTab.ShouldPayMonth;
            CurrentUGR.Cells["应付实付金额"].Value = m_modelFeeSJTruePayTab.ShouldRecvNotRecvMoney;

            CurrentUGR.Cells["对公公式"].Value = m_modelFeeSJTruePayTab.ToPubFormula;
            CurrentUGR.Cells["对私公式"].Value = m_modelFeeSJTruePayTab.ToPrivateFormula;
            CurrentUGR.Cells["经办人"].Value = m_modelFeeSJTruePayTab.JinBanPerson;
            CurrentUGR.Cells["收款人"].Value = m_modelFeeSJTruePayTab.RecvMonyePerson;

            //CurrentUGR.Cells["是否已提取至应收应付登记表"].Value = m_modelFeeSJTruePayTab.IsToRecvPayTab;
            //CurrentUGR.Cells["是否已提取至财务对帐"].Value = m_modelFeeSJTruePayTab.IsToFinCheckMoneyTab;


            CurrentUGR.Cells["备注"].Value = m_modelFeeSJTruePayTab.Note;

            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetailNoContractNoAndFac(nIndex);

            }

        }
        private void 复制增加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (!m_bInsertFlag)
                {
                    UltraGridRow CurrentUGRFrom;
                    CurrentUGRFrom = ultraGrid1.Rows[m_nDelIndex];
                    int nSelectFeeSJTruePayTabID = Convert.ToInt32(CurrentUGRFrom.Cells[0].Value.ToString(), 10);
                    
                    //string strTemp = CurrentUGRFrom.Cells["合同编号"].Value.ToString();
                    //DGBY.Model.LogRecTab modelLogRecTab = new DGBY.Model.LogRecTab();
                    //modelLogRecTab.LogDate = System.DateTime.Now.Date;
                    //modelLogRecTab.LogContent = Frm_Main.m_strLoginUser + "复制增加了合同号"
                    //    + strTemp + "的商检实付";
                    //m_bllLogRecTab.Add(modelLogRecTab);

                    m_modelFeeSJTruePayTab = m_bllFeeSJTruePayTab.GetModel(nSelectFeeSJTruePayTabID);
                    m_modelFeeSJTruePayTab.InputDate = System.DateTime.Now.Date;
                   // m_bCopyInsertFlag = true;
                    m_InsertType = InsertType.Common;
                    AddNewRow();

                }
            }
            catch
            {
                MessageBox.Show("没有选择记录，无法复制！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;
             //if (e.Button == MouseButtons.Left)
            //    contextMenuStrip1.Show(Cursor.Position);
            

        }

        private void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid1.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag = true;
            if (m_InsertType == InsertType.Common)
                InitNewAddRowValueCopyContract(e.Row.Index);
            else if (m_InsertType == InsertType.WithoutContractNo)
                InitNewAddRowValueNoContract(e.Row.Index);
            else if (m_InsertType == InsertType.WithoutContractNoAndFac)
                InitNewAddRowValueNoContractAndFac(e.Row.Index);


        }

        private void 删除本行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!Frm_Main.CheckPerm(this.Text, "删除"))
            {
                MessageBox.Show("对不起您没有该权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                if (ultraGrid1.ActiveRow != null)
                {
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

                    bool bRet = CheckModify(CurrentUGR);
                    if (!bRet)
                    {
                        MessageBox.Show("费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    bRet = IsFeeTruePayLocked(CurrentUGR);
                    if (bRet)
                    {
                        MessageBox.Show("实付费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    int nPaidanRegisterTabID = Convert.ToInt32(CurrentUGR.Cells["PaidanRegisterTabID"].Value.ToString(), 10);

                    if (nPaidanRegisterTabID < 0)
                    {
                        ultraGrid1.ActiveRow.Delete(false);
                        return;
                    }
   
                    DataSet ds = m_bllFeeSJTruePayTab.GetList("FeeSJTruePayTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'");
                    if (ds.Tables[0].Rows.Count > 1)
                    {
                        //int nFeeSJTruePayTabID = Convert.ToInt32(CurrentUGR.Cells["FeeSJTruePayTabID"].Value.ToString(), 10);


                        //ds = m_bllFeeSJTruePayTab.GetList("FeeSJTruePayTab.FeeSJTruePayTabID = '" + nFeeSJTruePayTabID.ToString() + "'");
                        ultraGrid1.ActiveRow.Delete(false);

                    }
                    else
                    {
                        MessageBox.Show("系统中该合同号只剩下一条记录，为系统自动生成，不能删除!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            if (!m_bInsertFlag)
            {
                int nSelectFeeSJTruePayTabID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells["FeeSJTruePayTabID"].Value.ToString(), 10);
                DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTabOld
                    = m_bllFeeSJTruePayTab.GetModel(nSelectFeeSJTruePayTabID);

                if (modelFeeSJTruePayTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    try
                    {

                        m_bllFeeSJTruePayTab.Delete(nSelectFeeSJTruePayTabID);

                        Frm_Main.Log2DB(this.Text, "删除", ultraGrid1.ActiveRow.Cells["合同编号"].Value.ToString());

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                MessageBox.Show("插入未完成，不能删除记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void SyncSBMonth()
        {
            DataSet dsTemp = m_bllHxdNoTab.ShowZhDjbRec("year(HxdNoTab.SBDate) > 1900");
            DataSet ds = m_bllFeeSJTruePayTab.GetList("ShouldPayMonth = 0 or TruePayMonth = 0");
            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
            {
                string strContractNo = ds.Tables[0].Rows[j]["合同编号"].ToString();
                int nFeeSJTruePayTabID = (int)ds.Tables[0].Rows[j]["FeeSJTruePayTabID"];
                DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTabOld
               = m_bllFeeSJTruePayTab.GetModel(nFeeSJTruePayTabID);

                DataRow[] Rows = dsTemp.Tables[0].Select("合同编号 = '" + strContractNo.ToString() + "'");
                if (Rows.Length > 0)
                {
                    DateTime dtSBMonth = (DateTime)Rows[0]["申报日期"];
                    int nSBMonth = dtSBMonth.Month;
                    modelFeeSJTruePayTabOld.ShouldPayMonth = nSBMonth;
                    modelFeeSJTruePayTabOld.TruePayMonth = nSBMonth;
                    m_bllFeeSJTruePayTab.Update(modelFeeSJTruePayTabOld);
                }

            }
           // ShowAllRecord("");
          //  MessageBox.Show("同步申报月完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void buttonSyncSBMonth_Click(object sender, EventArgs e)
        {
           

        }

        private void Frm_FeeSJTruePayTab_Load(object sender, EventArgs e)
        {
           // this.buttonStop.Enabled = false;
            InitComBox();
            //SyncSBMonth();
            ClassUltraGridCommonSet.InitUltraWinGridSetting(this.ultraGrid1);
        }

        private string MakeSJBianHao(string strDepart, string strComp
          , string strPuborPri, string strDate)
        {

            string strRet = "SJ" + ClassUtility.m_dicCn2EngDep[strDepart]
                + ClassUtility.m_dicCn2EngComp[strComp] + ClassUtility.m_dicCn2EngGS[strPuborPri] + strDate;

            if (m_strBianHaoTemp != "")
            {
                strRet += m_strBianHaoTemp;
            }
            else
            {

                DateTime dtTruePay = this.dateTimePicker1.Value.Date;
                DataSet dsTemp;
                //if (strPuborPri == "公")
                //    dsTemp = m_bllFeeSJTruePayTab.GetMaxList("TruePayDate = '" + dtTruePay + @"' and ToPubBianHao like '%" + strRet + "%'");
                //else

                dsTemp = m_bllFeeSJTruePayTab.GetMaxList("TruePayDate = '" + dtTruePay + "'");


                if (dsTemp.Tables[0].Rows.Count > 0)
                {
                    string strTest = "";

                    string strTestPub = dsTemp.Tables[0].Rows[0]["对公费用编号"].ToString();

                    string strTestPri = dsTemp.Tables[0].Rows[0]["对私费用编号"].ToString();

                    if (strTestPri.CompareTo(strTestPub) >= 0)
                        strTest = strTestPri;
                    else
                        strTest = strTestPub;


                    string strTemp = strTest.Remove(0, 8);
                    if (strTemp != "")
                    {
                        int nTemp = Convert.ToInt32(strTemp, 10) + 1;
                        strTemp = nTemp.ToString().PadLeft(3, '0'); ;
                    }
                    else
                    {
                        strTemp = "001";
                    }

                    strRet += strTemp;
                    m_strBianHaoTemp = strTemp;
                }
                else
                {
                    strRet = strRet + "001";
                    m_strBianHaoTemp = "001";
                }
            }

            return strRet;
        }

        private string MakeBianHaoWithoutLiuShuiNo(string strDepart, string strComp
          , string strPuborPri, string strDate)
        {

            string strRet = "SJ" + ClassUtility.m_dicCn2EngDep[strDepart]
                + ClassUtility.m_dicCn2EngComp[strComp] + ClassUtility.m_dicCn2EngGS[strPuborPri] + strDate;


           

            return strRet;
        }


        private void buttonBatchModifyDate_Click(object sender, EventArgs e)
        {
            m_strBianHaoTemp = "";
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的实付日期么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if(dr == DialogResult.Cancel)
                    return;



                //string strBianHao = MakeBianHao(ugr[0].Cells["部门"].Value.ToString()
                //       , ugr[0].Cells["经营单位"].Value.ToString()
                //       , "私"
                //        , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
                //        );

                string strBianHao = "";
                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTabOld
                                        = m_bllFeeSJTruePayTab.GetModel(nID);

                   
                    //strRet = strRet.Replace(m_strFacDirect, strIsFacDirect);

                    DateTime? dtTruePayOld = modelFeeSJTruePayTabOld.TruePayDate;

//                    DateTime dtTruePayOld = modelFeeSJTruePayTabOld.TruePayDate == null ?
//DateTime.Parse("1900-01-01") : (DateTime)modelFeeSJTruePayTabOld.TruePayDate.Value.Date;

                

                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

    //                if (((dtTruePayOld.CompareTo(modelFeeSJTruePayTabOld.TruePayDate) != 0) 
    //                    ||(modelFeeSJTruePayTabOld.ToPrivateBianHao == ""
    //&& modelFeeSJTruePayTabOld.ToPubBianHao == ""))
    //&& modelFeeSJTruePayTabOld.ShouldRecvNotRecvMoney > 0)
                    if(dtTruePayOld == null && modelFeeSJTruePayTabOld.ShouldRecvNotRecvMoney > 0)
                    {
                        modelFeeSJTruePayTabOld.TruePayDate = this.dateTimePicker1.Value.Date;
                        if (modelFeeSJTruePayTabOld.ToPrivateFormula != "")
                        {
                            strBianHao = MakeSJBianHao(ugr[0].Cells["部门"].Value.ToString()
                       , ugr[0].Cells["经营单位"].Value.ToString()
                       , "私"
                        , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd"));

                            string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                            string strRet = strBianHao.Insert(7, strIsFacDirect);

                            modelFeeSJTruePayTabOld.ToPrivateBianHao = strRet;
                            CurrentUGR.Cells["对私费用编号"].Value = strRet;

                        }
                        if (modelFeeSJTruePayTabOld.ToPubFormula != "")
                        {
                            strBianHao = MakeSJBianHao(ugr[0].Cells["部门"].Value.ToString()
                      , ugr[0].Cells["经营单位"].Value.ToString()
                      , "公"
                       , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd"));

                            string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                            string strRet = strBianHao.Insert(7, strIsFacDirect);
                            modelFeeSJTruePayTabOld.ToPrivateBianHao = strRet;
                            CurrentUGR.Cells["对公费用编号"].Value = strRet;

                        }

                        modelFeeSJTruePayTabOld.bitFiledReserv2 = false;//编号是否手动生成

                        CurrentUGR.Cells["实付日期"].Value = this.dateTimePicker1.Value.Date;
                        CurrentUGR.Cells["编号是否手动生成"].Value = false;

                        m_bllFeeSJTruePayTab.TransUpdate(modelFeeSJTruePayTabOld
                       , m_SQLStringList, m_arrayList, i);
                    }
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改实付日期", this.dateTimePicker1.Value.Date.ToString());
                MessageBox.Show("批量修改实付日期完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }


        //private string MakeBianHao(string strDepart, string strComp
        //    , string strPuborPri, string strDate)
        //{

        //    string strRet = "SJ" + ClassUtility.m_dicCn2EngDep[strDepart]
        //        + ClassUtility.m_dicCn2EngComp[strComp] + ClassUtility.m_dicCn2EngGS[strPuborPri] + strDate;


        //    DateTime dtTruePay = this.dateTimePicker1.Value.Date;
        //    DataSet dsTemp;
        //    if(strPuborPri == "公")
        //        dsTemp = m_bllFeeSJTruePayTab.GetMaxList("TruePayDate = '" + dtTruePay + @"' and ToPubBianHao like '%" + strRet + "%'");
        //    else
        //        dsTemp = m_bllFeeSJTruePayTab.GetMaxList("TruePayDate = '" + dtTruePay + @"' and ToPrivateBianHao like '%" + strRet + "%'");


        //    if (dsTemp.Tables[0].Rows.Count > 0)
        //    {
        //        string strTest = "";
        //        if(strPuborPri == "公")
        //            strTest = dsTemp.Tables[0].Rows[0]["对公费用编号"].ToString(); 
        //        else
        //            strTest = dsTemp.Tables[0].Rows[0]["对私费用编号"].ToString();

        //        string strTemp = strTest.Remove(0, strRet.Length);
        //        if (strTemp != "")
        //        {
        //            int nTemp = Convert.ToInt32(strTemp, 10) + 1;
        //            strTemp = nTemp.ToString().PadLeft(3, '0'); ;
        //        }
        //        else
        //        {
        //            strTemp = "001";
        //        }

        //        strRet += strTemp;
        //    }
        //    else
        //    {
        //        strRet = strRet + "001";
        //    }

        //    return strRet;
        //}

//        private string MakeBianHao(string strDepart, string strComp
//   , string strPuborPri, string strDate, int nSeqNo)
//        {
//            string strRet = "SJ" + ClassUtility.m_dicCn2EngDep[strDepart]
//+ ClassUtility.m_dicCn2EngComp[strComp] + ClassUtility.m_dicCn2EngGS[strPuborPri] + strDate + nSeqNo.ToString().PadLeft(3, '0');

//            // string strRet = "DC" + strDepart + strComp + strPuborPri + strDate ;
//            return strRet;
//        }

        //private void buttonBatchModifyDate_Click(object sender, EventArgs e)
        //{

        //    m_SQLStringList.Clear();
        //    m_arrayList.Clear();
        //    DateTime dtTruePay = this.dateTimePicker1.Value.Date;
        //    DataSet dsTemp = m_bllFeeSJTruePayTab.GetList("TruePayDate = '" + dtTruePay + @"' and (ToPubBianHao != '' or ToPrivateBianHao != '')");
        //    int nCount = dsTemp.Tables[0].Rows.Count + 1;
        //    try
        //    {
        //        Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

        //        //string strRet = MakeBianHao(ugr[0].Cells["部门"].Value.ToString()
        //        //       , ugr[0].Cells["经营单位"].Value.ToString()
        //        //       , "私"
        //        //        , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd"));

        //        for (int i = 0; i < ugr.Length; i++)
        //        {

        //            int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
        //            DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTabOld
        //                                = m_bllFeeSJTruePayTab.GetModel(nID);

        //            DateTime dtTruePayOld = modelFeeSJTruePayTabOld.TruePayDate == null ?
        //   DateTime.Parse("1900-01-01") : (DateTime)modelFeeSJTruePayTabOld.TruePayDate.Value.Date;

        //            modelFeeSJTruePayTabOld.TruePayDate = this.dateTimePicker1.Value.Date;

        //            UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

        //            if (modelFeeSJTruePayTabOld.ToPrivateBianHao == ""
        //                && modelFeeSJTruePayTabOld.ToPubBianHao == ""
        //                && modelFeeSJTruePayTabOld.ShouldRecvNotRecvMoney > 0)
        //            {
        //                if (modelFeeSJTruePayTabOld.ToPrivateFormula != "")
        //                {
        //                    string strRet = MakeBianHao(ugr[i].Cells["部门"].Value.ToString()
        //                   , ugr[i].Cells["经营单位"].Value.ToString()
        //                   , "私"
        //                    , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
        //                    , nCount);

        //                    modelFeeSJTruePayTabOld.ToPrivateBianHao = strRet;
        //                    //nCount++;
        //                    CurrentUGR.Cells["对私费用编号"].Value = strRet;
        //                }
        //                else if (modelFeeSJTruePayTabOld.ToPubFormula != "")
        //                {
        //                    string strRet = MakeBianHao(ugr[i].Cells["部门"].Value.ToString()
        //                   , ugr[i].Cells["经营单位"].Value.ToString()
        //                   , "公"
        //                    , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
        //                    , nCount);

        //                    modelFeeSJTruePayTabOld.ToPubBianHao = strRet;
        //                    //nCount++;
        //                    CurrentUGR.Cells["对公费用编号"].Value = strRet;
        //                }

        //            }
        //            else if (dtTruePayOld.CompareTo(modelFeeSJTruePayTabOld.TruePayDate) != 0
        //                && modelFeeSJTruePayTabOld.ShouldRecvNotRecvMoney > 0)
        //            {
        //                if (modelFeeSJTruePayTabOld.ToPrivateFormula != "")
        //                {
        //                    string strRet = MakeBianHao(ugr[i].Cells["部门"].Value.ToString()
        //                   , ugr[i].Cells["经营单位"].Value.ToString()
        //                   , "私"
        //                    , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
        //                    , nCount);

        //                    modelFeeSJTruePayTabOld.ToPrivateBianHao = strRet;
        //                    //nCount++;
        //                    CurrentUGR.Cells["对私费用编号"].Value = strRet;
        //                }
        //                else if (modelFeeSJTruePayTabOld.ToPubFormula != "")
        //                {
        //                    string strRet = MakeBianHao(ugr[i].Cells["部门"].Value.ToString()
        //                   , ugr[i].Cells["经营单位"].Value.ToString()
        //                   , "公"
        //                    , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
        //                    , nCount);
        //                    modelFeeSJTruePayTabOld.ToPubBianHao = strRet;
        //                    //nCount++;
        //                    CurrentUGR.Cells["对公费用编号"].Value = strRet;
        //                }

        //            }


        //            //if (modelFeeSJTruePayTabOld.ToPrivateFormula != ""
        //            //    && modelFeeSJTruePayTabOld.ShouldRecvNotRecvMoney > 0)
        //            //{

        //            //    modelFeeSJTruePayTabOld.ToPrivateBianHao = strRet;
        //            //    CurrentUGR.Cells["对私费用编号"].Value = strRet;
        //            //}
        //            //else if (modelFeeSJTruePayTabOld.ToPubFormula != ""
        //            //    && modelFeeSJTruePayTabOld.ShouldRecvNotRecvMoney > 0)
        //            //{

        //            //    modelFeeSJTruePayTabOld.ToPubBianHao = strRet;
        //            //    CurrentUGR.Cells["对公费用编号"].Value = strRet;
        //            //}
        //            m_bllFeeSJTruePayTab.TransUpdate(modelFeeSJTruePayTabOld
        //                , m_SQLStringList, m_arrayList, i);

        //            CurrentUGR.Cells["实付日期"].Value = this.dateTimePicker1.Value.Date;
        //        }

        //        m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
        //        MessageBox.Show("批量修改实付日期完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        //    }
        //    catch (System.Exception ex)
        //    {
        //        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //        return;
        //    }

        //}
        private void buttonExportExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonSetEmpty_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                bool bCanModify = true;

                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                
                DialogResult dr = MessageBox.Show("确实要批量置空以下" + ugr.Length.ToString() + "条记录的实付日期么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;

                List<string> lstDT = new List<string>();
                for (int i = 0; i < ugr.Length; i++)
                {
                    string strTemp = ugr[i].Cells["对公费用编号"].Value.ToString() + ugr[i].Cells["对私费用编号"].Value.ToString();

                    if (strTemp != "" && lstDT.Count == 0)
                        lstDT.Add(strTemp);
                    else if (lstDT.Count > 0 && !lstDT.Contains(strTemp))
                    {
                        bCanModify = false;
                        break;
                    }

                }

                if (!bCanModify)
                {
                    MessageBox.Show("费用编号不相同，不能批量置空实付日期!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                for (int i = 0; i < ugr.Length; i++)
                {
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];
                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTabOld
                                        = m_bllFeeSJTruePayTab.GetModel(nID);
                    modelFeeSJTruePayTabOld.TruePayDate = null;
                    modelFeeSJTruePayTabOld.ToPrivateBianHao = "";
                    modelFeeSJTruePayTabOld.ToPubBianHao = "";
                    modelFeeSJTruePayTabOld.bitFiledReserv2 = false;
             
                    m_bllFeeSJTruePayTab.TransUpdate(modelFeeSJTruePayTabOld
                        , m_SQLStringList, m_arrayList, i);
                    CurrentUGR.Cells["对私费用编号"].Value = "";
                    CurrentUGR.Cells["对公费用编号"].Value = "";
                    CurrentUGR.Cells["实付日期"].Value = DBNull.Value;
                    CurrentUGR.Cells["编号是否手动生成"].Value = false;
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量置空实付日期", "");
                MessageBox.Show("批量置空实付日期完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Frm_FeeSJRecvMoneyPersonTab frm_FeeSJRecvMoneyPersonTab = new Frm_FeeSJRecvMoneyPersonTab();
            frm_FeeSJRecvMoneyPersonTab.StartPosition = FormStartPosition.CenterScreen;
            frm_FeeSJRecvMoneyPersonTab.ShowDialog();
        }

        private void buttonBatchModRecvMoneyP_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

         

                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTabOld
                                        = m_bllFeeSJTruePayTab.GetModel(nID);
                    modelFeeSJTruePayTabOld.RecvMonyePerson = this.comboBoxRecvMoneyPerson.Text;
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];
                   
                    m_bllFeeSJTruePayTab.TransUpdate(modelFeeSJTruePayTabOld
                        , m_SQLStringList, m_arrayList, i);

                    CurrentUGR.Cells["收款人"].Value = this.comboBoxRecvMoneyPerson.Text;
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改收款人", this.comboBoxRecvMoneyPerson.Text);
                MessageBox.Show("批量修改收款人完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void 增加一行ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 增加无合同号记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!m_bInsertFlag)
            {
                m_InsertType = InsertType.WithoutContractNo;
                UltraGridRow CurrentUGR = ultraGrid1.Rows[m_nDelIndex];
                DataSet ds;
                string strFac = CurrentUGR.Cells["工厂简称"].Value.ToString();
                if (ClassInputValid.IsNum(strFac))
                {
                    m_nFacID = Convert.ToInt32(CurrentUGR.Cells["工厂简称"].Value.ToString(), 10);
                }
                else
                {
                    ds = m_bllFactoryDataTab.GetList("FactoryNameJX = '" + strFac + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        m_nFacID = Convert.ToInt32(ds.Tables[0].Rows[0]["FactoryDataTabID"].ToString(), 10);
                    }
                }
                AddNewRow();
                //  row.Cells[2] = DateTime.Parse("1900-01-01");
            }
        }

        private void 增加无工厂及合同号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!m_bInsertFlag)
            {
                m_InsertType = InsertType.WithoutContractNoAndFac;
                AddNewRow();
                //  row.Cells[2] = DateTime.Parse("1900-01-01");
            }
        }

        private void ultraGrid1_AfterCellUpdate(object sender, CellEventArgs e)
        {
             if (e.Cell.Column.Key == "对公公式"
                || e.Cell.Column.Key == "对私公式")
            {
                if (m_bInsertFlag)
                    return;
                int nIndex = e.Cell.Row.Index;
                if (nIndex < 0)
                    return;
                UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
                if (e.Cell.Value.ToString() == "")
                    return;
                Decimal dJinE = Convert.ToDecimal(e.Cell.Value.ToString());
                int nFeeSJTruePayTabID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTab
                    = m_bllFeeSJTruePayTab.GetModel(nFeeSJTruePayTabID);
                modelFeeSJTruePayTab.ShouldRecvNotRecvMoney = dJinE;
                m_bllFeeSJTruePayTab.Update(modelFeeSJTruePayTab);

                //DataSet ds = m_bllFeeSJTruePayTab.GetShouldRecvMoney("FeeSJTruePayTab.FeeSJTruePayTabID = '" + nFeeSJTruePayTabID + "'");
                //if (ds.Tables[0].Rows[0]["应收金额"].ToString() != "")
                //    CurrentUGR.Cells["应收金额"].Value = (Decimal)ds.Tables[0].Rows[0]["应收金额"];
                //else
                //    CurrentUGR.Cells["应收金额"].Value = 0;
            }
        }

        private void buttonModYearMon_Click(object sender, EventArgs e)
        {
            if (!ClassInputValid.IsNum(textBoxYearMonth.Text))
            {
                errorProvider1.SetError(textBoxYearMonth, "必须输入数字");
                return;
            }
            int nYearMon = Convert.ToInt32(textBoxYearMonth.Text,10);
            if (nYearMon < 201001 || nYearMon > 219912)
            {
                errorProvider1.SetError(textBoxYearMonth, "必须正确的年月，形如201206");
                return;
            }

            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的应收费用计提年月么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;
                

                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTabOld
                                        = m_bllFeeSJTruePayTab.GetModel(nID);
                    modelFeeSJTruePayTabOld.ShouldPayMonth = nYearMon;
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                    m_bllFeeSJTruePayTab.TransUpdate(modelFeeSJTruePayTabOld
                        , m_SQLStringList, m_arrayList, i);

                    CurrentUGR.Cells["应收费用计提年月"].Value = nYearMon.ToString();
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改计提年月", nYearMon.ToString());
                MessageBox.Show("批量修改应收费用计提年月完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void 清空本条记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确实要清空该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                UltraGridRow CurrentUGR;

                CurrentUGR = ultraGrid1.Rows[nIndex];

                bool bRet = CheckModify(CurrentUGR);
                if (!bRet)
                {
                    MessageBox.Show("费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                bRet = IsFeeTruePayLocked(CurrentUGR);
                if (bRet)
                {
                    MessageBox.Show("实付费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                ResetUltraGridRow(nIndex);
              
                int nFeeSJTruePayTabID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                ResetRecord(nFeeSJTruePayTabID);
                m_bClearing = true;
            }
            else
            {
                MessageBox.Show("请选中要清空的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量置空以下" + ugr.Length.ToString() + "条记录的应收费用计提年月么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;


                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTabOld
                                        = m_bllFeeSJTruePayTab.GetModel(nID);
                    modelFeeSJTruePayTabOld.ShouldPayMonth = 0;
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                    m_bllFeeSJTruePayTab.TransUpdate(modelFeeSJTruePayTabOld
                        , m_SQLStringList, m_arrayList, i);

                    CurrentUGR.Cells["应收费用计提年月"].Value = "0";
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量置空计提年月", "");
                MessageBox.Show("批量修改应收费用计提年月完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        private bool IsLiuShuiNo(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[0-9]{3}$");
        }
        private bool IsLiuShuiNo(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return false;
            }
            else if (!IsLiuShuiNo(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须001-999之间的流水号");
                return false;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return true;
            }
        }
        private void buttonBatchModFeeNo_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                if (!IsLiuShuiNo(this.errorProvider1, this.textBoxLiuShuiNo))
                    return;
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的费用编号么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;

                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTabOld
                                        = m_bllFeeSJTruePayTab.GetModel(nID);

                    DateTime? dtTruePayOld = modelFeeSJTruePayTabOld.TruePayDate;
                    if (dtTruePayOld != null)
                    {
                        MessageBox.Show("原记录实付日期不为空，不能修改，若要修改，请先置空实付日期!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    modelFeeSJTruePayTabOld.TruePayDate = this.dateTimePicker1.Value.Date;

                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                    if (modelFeeSJTruePayTabOld.ToPrivateBianHao == ""
                        && modelFeeSJTruePayTabOld.ToPubBianHao == ""
                        && modelFeeSJTruePayTabOld.ShouldRecvNotRecvMoney > 0)
                    {
                        if (modelFeeSJTruePayTabOld.ToPrivateFormula != ""
                            && modelFeeSJTruePayTabOld.ShouldRecvNotRecvMoney > 0)
                        {
                            string strBianHao = MakeBianHaoWithoutLiuShuiNo(ugr[0].Cells["部门"].Value.ToString()
                  , ugr[0].Cells["经营单位"].Value.ToString()
                  , "私"
                   , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
                   );

                            string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                            string strRet = strBianHao.Insert(7, strIsFacDirect) + this.textBoxLiuShuiNo.Text;

                            modelFeeSJTruePayTabOld.ToPrivateBianHao = strRet;
                            CurrentUGR.Cells["对私费用编号"].Value = strRet;
                        }
                        else if (modelFeeSJTruePayTabOld.ToPubFormula != ""
                            && modelFeeSJTruePayTabOld.ShouldRecvNotRecvMoney > 0)
                        {
                            string strBianHao = MakeBianHaoWithoutLiuShuiNo(ugr[0].Cells["部门"].Value.ToString()
                  , ugr[0].Cells["经营单位"].Value.ToString()
                  , "公"
                   , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
                   );

                            string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                            string strRet = strBianHao.Insert(7, strIsFacDirect) + this.textBoxLiuShuiNo.Text;

                            modelFeeSJTruePayTabOld.ToPubBianHao = strRet;
                            CurrentUGR.Cells["对公费用编号"].Value = strRet;
                        }


                        modelFeeSJTruePayTabOld.bitFiledReserv2 = true;//编号是否手动生成
                        m_bllFeeSJTruePayTab.TransUpdate(modelFeeSJTruePayTabOld
                            , m_SQLStringList, m_arrayList, i);

                        CurrentUGR.Cells["实付日期"].Value = this.dateTimePicker1.Value.Date;
                        CurrentUGR.Cells["编号是否手动生成"].Value = true;
                    }


                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改费用编号", this.textBoxLiuShuiNo.Text);
                MessageBox.Show("批量修改费用编号完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        private void AddFeeBGTruePayTab(int nPaidanRegisterTabID)
        {
            DataSet ds = m_bllFeeBGTruePayTab.GetListInfo("FeeBGTruePayTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID + "'");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return;
            }



            DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTab = new DGBY.Model.FeeBGTruePayTab();

            modelFeeBGTruePayTab.PaidanRegisterTabID = nPaidanRegisterTabID;
            modelFeeBGTruePayTab.ToPubBianHao = "";
            modelFeeBGTruePayTab.ToPrivateBianHao = "";
            modelFeeBGTruePayTab.SBDate = DateTime.Parse("1900-01-01");
            //modelFeeBGTruePayTab.TruePayDate = DateTime.Parse("1900-01-01");
            //modelFeeBGTruePayTab.InputDate = DateTime.Parse("1900-01-01"); 
            modelFeeBGTruePayTab.TruePayMonth = 0;
            modelFeeBGTruePayTab.ShouldPayMonth = 0;
            modelFeeBGTruePayTab.PrivateTruePayMoney = 0;
            modelFeeBGTruePayTab.PubTruePayMoney = 0;

            modelFeeBGTruePayTab.ToPubFormula = "";
            modelFeeBGTruePayTab.ToPrivateFormula = "";

            modelFeeBGTruePayTab.JinBanPerson = "";
            modelFeeBGTruePayTab.RecvMonyePerson = "";

            modelFeeBGTruePayTab.IsToRecvPayTab = 0;
            modelFeeBGTruePayTab.IsToFinCheckMoneyTab = 0;
            modelFeeBGTruePayTab.Note = "";

            modelFeeBGTruePayTab.bitFiledReserv1 = false;
            modelFeeBGTruePayTab.bitFiledReserv2 = false;

            modelFeeBGTruePayTab.stringFiledReserv1 = "";
            modelFeeBGTruePayTab.stringFiledReserv2 = "";
            modelFeeBGTruePayTab.stringFiledReserv3 = "";
            modelFeeBGTruePayTab.stringFiledReserv4 = "";

            modelFeeBGTruePayTab.intFiledReserv1 = 33;
            modelFeeBGTruePayTab.intFiledReserv2 = 10;
            modelFeeBGTruePayTab.intFiledReserv3 = 0;
            modelFeeBGTruePayTab.intFiledReserv4 = 0;

            DateTime dtNow = System.DateTime.Now.Date;
            modelFeeBGTruePayTab.datetimeFiledReserv1 = dtNow;
            modelFeeBGTruePayTab.datetimeFiledReserv2 = dtNow;

            modelFeeBGTruePayTab.decimalFiledReserv1 = 0;
            modelFeeBGTruePayTab.decimalFiledReserv2 = 0;
            modelFeeBGTruePayTab.decimalFiledReserv3 = 0;
            modelFeeBGTruePayTab.decimalFiledReserv4 = 0;
            // modelFeeBGTruePayTab.SjDate = System.DateTime.Now.Date;
            m_bllFeeBGTruePayTab.Add(modelFeeBGTruePayTab);

        }
        private void AddFeeSJTruePayTab(int nPaidanRegisterTabID)
        {
            DataSet ds = m_bllFeeSJTruePayTab.GetListInfo("FeeSJTruePayTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID + "'");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                MessageBox.Show("该合同号已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            ds = m_bllPaidanRegisterTab.GetListInfo("PaidanRegisterTabID = '" + nPaidanRegisterTabID + "'");



            DGBY.Model.FeeSJTruePayTab modelFeeSJTruePayTab = new DGBY.Model.FeeSJTruePayTab();

            modelFeeSJTruePayTab.FactoryDataTabID = (int)ds.Tables[0].Rows[0]["FactoryDataTabID"];
            modelFeeSJTruePayTab.BYCompanyTabID = (int)ds.Tables[0].Rows[0]["BYCompanyTabID"];
            modelFeeSJTruePayTab.BYDepartTabID = (int)ds.Tables[0].Rows[0]["BYDepartTabID"];

            modelFeeSJTruePayTab.PaidanRegisterTabID = nPaidanRegisterTabID;
            modelFeeSJTruePayTab.ToPubBianHao = "";
            modelFeeSJTruePayTab.ToPrivateBianHao = "";

            //modelFeeSJTruePayTab.TruePayDate = DateTime.Parse("1900-01-01");
            //modelFeeSJTruePayTab.InputDate = DateTime.Parse("1900-01-01"); 
            modelFeeSJTruePayTab.TruePayMonth = 0;
            modelFeeSJTruePayTab.ShouldPayMonth = 0;
            modelFeeSJTruePayTab.ShouldRecvNotRecvMoney = 0;
            modelFeeSJTruePayTab.ShouldRecvMoney = 0;

            modelFeeSJTruePayTab.ToPubFormula = "";
            modelFeeSJTruePayTab.ToPrivateFormula = "";

            modelFeeSJTruePayTab.JinBanPerson = "";
            modelFeeSJTruePayTab.RecvMonyePerson = "";

            DateTime dtNow = System.DateTime.Now.Date;
            modelFeeSJTruePayTab.IsToRecvPayTab = 0;
            modelFeeSJTruePayTab.IsToFinCheckMoneyTab = 0;
            modelFeeSJTruePayTab.Note = "";
            modelFeeSJTruePayTab.SjDate = dtNow;

            modelFeeSJTruePayTab.bitFiledReserv1 = false;
            modelFeeSJTruePayTab.bitFiledReserv2 = false;

            modelFeeSJTruePayTab.stringFiledReserv1 = "";
            modelFeeSJTruePayTab.stringFiledReserv2 = "";
            modelFeeSJTruePayTab.stringFiledReserv3 = "";
            modelFeeSJTruePayTab.stringFiledReserv4 = "";

            modelFeeSJTruePayTab.intFiledReserv1 = 0;
            modelFeeSJTruePayTab.intFiledReserv2 = 0;
            modelFeeSJTruePayTab.intFiledReserv3 = 0;

            modelFeeSJTruePayTab.datetimeFiledReserv1 = dtNow;
            modelFeeSJTruePayTab.datetimeFiledReserv2 = dtNow;

            modelFeeSJTruePayTab.decimalFiledReserv1 = 0;
            modelFeeSJTruePayTab.decimalFiledReserv2 = 0;
            modelFeeSJTruePayTab.decimalFiledReserv3 = 0;
            modelFeeSJTruePayTab.decimalFiledReserv4 = 0;

            int nID = m_bllFeeSJTruePayTab.Add(modelFeeSJTruePayTab);
            ShowAllRecord("FeeSJTruePayTab.FeeSJTruePayTabID = '" + nID.ToString() + "'");
            MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string strCode = this.textBoxContractCode.Text;
            DataSet ds = m_bllPaidanRegisterTab.GetListInfo("ContractCode = '" + strCode + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                int nPaidanRegisterTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
                AddFeeBGTruePayTab(nPaidanRegisterTabID);
                AddFeeSJTruePayTab(nPaidanRegisterTabID);
                Frm_Main.Log2DB(this.Text, "找回", strCode);
            }
            else
            {
                MessageBox.Show("不存在该合同号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }


    }
}
